﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using shfa.snomtastic.admin.components;
using shfa.snomtastic.data.business;
using Telerik.Web.UI;
using shfa.snomtastic.data;


namespace shfa.snomtastic.admin.editcontrols
{
    public partial class eSettings : EditBase
    {
        #region Public Members

        public RadComboBox ddCategory { get; set; }
        public RadComboBox ddSubCategory { get; set; }



        public Guid ActiveItemId
        {
            get { return (hfItemId.Value != string.Empty) ? new Guid(hfItemId.Value) : Guid.Empty; }
            set { hfItemId.Value = value.ToString(); }
        }

        public Guid CategoryId
        {
            get { return (hfCategoryId.Value != string.Empty) ? new Guid(hfCategoryId.Value) : Guid.Empty; }
            set { hfCategoryId.Value = value.ToString(); }
        }

        public Guid SubCategoryId
        {
            get { return (hfSubCategoryId.Value != string.Empty) ? new Guid(hfSubCategoryId.Value) : Guid.Empty; }
            set { hfSubCategoryId.Value = value.ToString(); }
        }


        #endregion

        #region Page

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGridItems();
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            if (ItemId != Guid.Empty)
                ActiveItemId = ItemId;

            rgListItems.ItemCommand += new GridCommandEventHandler(rgListItems_ItemCommand);

            ScriptManager.RegisterClientScriptBlock(this, typeof(EditBase), "ClientEvents", common.DblClickScript, true);


            SubcategoryController sc = new SubcategoryController();
            SubcategoryInfo sci = sc.SelectSubcategory(ActiveItemId);

            CategoryController cc = new CategoryController();
            CategoryInfo ci = cc.SelectCategory(ActiveItemId);

            if (sci != null)
            {
                SubCategoryId = sci.SubcategoryId;

            }
            if (ci != null)
            {
                CategoryId = ci.CategoryId;
            }
        }

        #endregion

        #region Event Handlers

        protected void rgListItems_ItemCommand(object source, GridCommandEventArgs e)
        {

            if (e.CommandName == "Sort" | e.CommandName == "Filter")
            {
                BindGridItems();

            }

            if (e.CommandName == "Edit")
            {
                e.Canceled = true;
                Guid id = new Guid(rgListItems.Items[e.Item.ItemIndex].GetDataKeyValue("SettingId").ToString());
                RaiseNavEvent(this, new NavEventArgs("eSetting", id));

            }
        }

        #endregion


        public void BindGridItems()
        {
            SettingController sc = new SettingController();
            List<SettingInfo> siList;
            if (SubCategoryId != Guid.Empty)
            {
                siList = sc.SelectSettingsBySubcategory(SubCategoryId, "Name Asc");
            }
            else if (CategoryId != Guid.Empty)
            {
                siList = sc.SelectSettingsByCategory(CategoryId, "Name Asc");
            }
            else
            {
                siList = sc.SelectSettings("Name Asc");
            }



            foreach (SettingInfo si in siList)
            {
                si.FillDetails();
            }

            rgListItems.DataSource = siList;
            rgListItems.DataBind();


        }


        #region Action Events
        public void DeleteItem(Guid id)
        {
            SettingController sc = new SettingController();
            sc.DeleteSetting(id);
            RaiseNavEvent(this, new NavEventArgs("eSettings", id));
        }


        #endregion

     
        #region EditBase

        public override void EditBase_ActionEvent(object sender, ActionClickArgs e)
        {
            switch (e.ActionName)
            {
                case "Add":
                    RaiseNavEvent(this, new NavEventArgs("eSetting", new Guid()));
                    break;
                case "Edit":
                    if (rgListItems.SelectedValue != null)
                    {
                        RaiseNavEvent(this, new NavEventArgs("eSetting", new Guid(rgListItems.SelectedValue.ToString())));
                    }
                    break;
                case "Delete":
                    if (rgListItems.SelectedValue != null)
                    {
                        DeleteItem(new Guid(rgListItems.SelectedValue.ToString()));
                    }
                    break;
                default:
                    break;
            }
        }

        public override List<ActionInfo> ActionList
        {
            get
            {
                List<ActionInfo> al = new List<ActionInfo>();
                al.Add(new ActionInfo("Add", "Add Setting", "~/images/icons/add.png"));
                al.Add(new ActionInfo("Edit", "Edit Setting", "~/images/icons/edit.png"));
                al.Add(new ActionInfo("Delete", "Delete Setting", "~/images/icons/delete.png", "Are you sure you want to delete this setting?", true));
                return al;
            }
        }

        #endregion


    }
}